package com.ruoyi.web.controller.system.city;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import cn.hutool.core.util.IdUtil;
import com.ruoyi.system.domain.city.CityTrade;
import com.ruoyi.system.service.city.ICityTradeService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.city.CityTradeSecond;
import com.ruoyi.system.service.city.ICityTradeSecondService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 二级行业Controller
 *
 * @author cjq
 * @date 2023-07-24
 */
@RestController
@RequestMapping("/system/city/secondTrade")
public class CityTradeSecondController extends BaseController {
    @Autowired
    private ICityTradeSecondService cityTradeSecondService;

    @Autowired
    private ICityTradeService cityTradeService;


    /**
     * 查询二级行业列表
     */
    @PreAuthorize("@ss.hasPermi('city:secondTrade:list')")
    @GetMapping("/list")
    public TableDataInfo list(CityTradeSecond cityTradeSecond) {
        startPage();
        List<CityTradeSecond> list = cityTradeSecondService.selectCityTradeSecondList(cityTradeSecond);
        return getDataTable(list);
    }

    /**
     * 导出二级行业列表
     */
    @PreAuthorize("@ss.hasPermi('city:secondTrade:export')")
    @Log(title = "二级行业", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, CityTradeSecond cityTradeSecond) {
        List<CityTradeSecond> list = cityTradeSecondService.selectCityTradeSecondList(cityTradeSecond);
        ExcelUtil<CityTradeSecond> util = new ExcelUtil<CityTradeSecond>(CityTradeSecond.class);
        util.exportExcel(response, list, "二级行业数据");
    }

    /**
     * 获取二级行业详细信息
     */
    @PreAuthorize("@ss.hasPermi('city:secondTrade:query')")
    @GetMapping(value = "/{tradeSecondId}")
    public AjaxResult getInfo(@PathVariable("tradeSecondId") Long tradeSecondId) {
        return success(cityTradeSecondService.selectCityTradeSecondByTradeSecondId(tradeSecondId));
    }

    /**
     * 新增二级行业
     */
    @PreAuthorize("@ss.hasPermi('city:secondTrade:add')")
    @Log(title = "二级行业", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody CityTradeSecond cityTradeSecond) {
        cityTradeSecond.setTradeSecondId(IdUtil.getSnowflakeNextId());
        CityTrade cityTrade = cityTradeService.selectCityTradeByTradeId(cityTradeSecond.getTradeId());
        if (cityTrade != null) {
            cityTradeSecond.setTradeName(cityTrade.getTradeName());
        }
        return toAjax(cityTradeSecondService.insertCityTradeSecond(cityTradeSecond));
    }

    /**
     * 修改二级行业
     */
    @PreAuthorize("@ss.hasPermi('city:secondTrade:edit')")
    @Log(title = "二级行业", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody CityTradeSecond cityTradeSecond) {
        CityTrade cityTrade = cityTradeService.selectCityTradeByTradeId(cityTradeSecond.getTradeId());
        if (cityTrade != null) {
            cityTradeSecond.setTradeName(cityTrade.getTradeName());
        }
        return toAjax(cityTradeSecondService.updateCityTradeSecond(cityTradeSecond));
    }

    /**
     * 删除二级行业
     */
    @PreAuthorize("@ss.hasPermi('city:secondTrade:remove')")
    @Log(title = "二级行业", businessType = BusinessType.DELETE)
    @DeleteMapping("/{tradeSecondIds}")
    public AjaxResult remove(@PathVariable Long[] tradeSecondIds) {
        return toAjax(cityTradeSecondService.deleteCityTradeSecondByTradeSecondIds(tradeSecondIds));
    }
}
